package com.sea.data.service.impl;


import com.codecrane.core.util.CommonUtils;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.supply.entity.Buy;
import com.sea.data.entity.Order;
import com.sea.data.entity.OrderDetail;
import com.sea.data.mapper.OrderMapper;
import com.sea.supply.service.BuyService;
import com.sea.data.service.OrderDetailService;
import com.sea.data.service.OrderService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.mapper.ChannelMapper;
import com.sea.product.service.ProductService;
import com.sea.supply.mapper.ProductPriceMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductPriceMapper productPriceMapper;
    @Autowired
    private BuyService buyService;

    @Override
    public List<Order> findByCnd(Order order) {
        return orderMapper.findByCnd(order);
    }

    @Override
    public int save(Order order) {
        return orderMapper.insert(order);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<OrderDetail> list = BatchImportUtils.iteratorForOrderDetail(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                Order order=new Order();
                //生成请购单号
                String orderNo = CommonUtils.fill(orderMapper.getMaxId(), 5, 7);
                order.setOrderNo("IR"+orderNo);
                order.setOrderMember(userRealName);
                //默认未审核
                order.setOrderStatus(1);
                save(order);
                for (OrderDetail orderDetail : list) {
                    orderDetail.setOrderId(order.getId());
                    String productChannelName = orderDetail.getOrderChannelName();
                    //查询渠道
                    Channel channel = channelMapper.findByChannelName(productChannelName);
                    if (null == channel) {
                        throw new Exception("未查询到该渠道:" + productChannelName);
                    }
                    //查询SKU
                    Product pro = productService.findBySku(orderDetail.getOrderSku());
                    if (null == pro) {
                        throw new Exception("未查询到该SKU:" + orderDetail.getOrderSku());
                    }
                    if(StringUtils.isNotBlank(pro.getProductPropertyName()) && pro.getProductPropertyName().contains("汰")){
                        throw new Exception("该SKU已淘汰,请勿下单:" + orderDetail.getOrderSku());
                    }

                    orderDetail.setOrderProductPrice(productPriceMapper.findMinPriceBySku(orderDetail.getOrderSku()));
                    orderDetailService.save(orderDetail);
                }

            }
        }
    }

    @Override
    public int delete(Integer id) {
        return orderMapper.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(Order order) {
        //如果是审核通过则新增待采购
        if(order.getOrderStatus().equals(3)) {
            List<OrderDetail> list = orderDetailService.findByOrderId(order.getId());
            for(OrderDetail orderDetail:list){
                Buy buy=new Buy();
                buy.setOrderId(orderDetail.getOrderId());
                buy.setDetailId(orderDetail.getId());
                buy.setBuyNumber(orderDetail.getOrderNumber());
                buy.setBuyOrderDate(orderDetail.getOrderDate());
                buyService.save(buy);
            }
        }
        return orderMapper.update(order);
    }

    @Override
    public List<Order> findStatusNumber(Integer status) {
        List<Order> list=new ArrayList<>();
        List<Order> statusList = orderMapper.findStatusNumber();
        if(null!=status && status>0){
            for(Order order:statusList){
                if(order.getOrderStatus().equals(status)){
                    list.add(order);
                    break;
                }
            }
            return list;
        }else{
            return statusList;
        }
    }
}
